package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.Property;
import cn.wolfcode.shop.mapper.PropertyMapper;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.service.IPropertyValueService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by wolfcode on 2018/8/17.
 */
@Service@Transactional
public class PropertyServiceImpl implements IPropertyService {
    @Autowired
    private PropertyMapper propertyMapper;
    @Autowired
    private IPropertyValueService propertyValueService;
    public List<Property> queryByCatalogId(Long catalogId) {
        return propertyMapper.queryByCatalogId(catalogId);
    }

    public void saveOrUpdate(Property property) {
        if(property.getId()==null){
            propertyMapper.insert(property);
        }else{
            propertyMapper.updateByPrimaryKey(property);
        }
    }

    public void delete(Long propertyId) {
        propertyMapper.deleteByPrimaryKey(propertyId);
    }

    public Integer queryCountByCatalogId(Long id) {
        return propertyMapper.queryCountByCatalogId(id);
    }

    public List<Property> queryByCatalogIdForProduct(Long catalogId) {
        List<Property> properties = propertyMapper.queryByCatalogId(catalogId);
        for(Property property:properties){
            if(property.getType()==Property.TYPE_SELECT){
                property.setPropertyValueList(propertyValueService.queryByPropertyId(property.getId()));
            }
        }
        return properties;
    }
}
